package com.schoolai.schoolset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.schoolai.entity.SchoolSetNews;
import com.schoolai.schoolset.mapper.SchoolSetNewsMapper;
import com.schoolai.schoolset.req.SetNewsREQ;
import com.schoolai.schoolset.service.ISchoolSetNewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolai.util.base.Result;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bobby
 * @since 2025-10-30
 */
@Service
@Slf4j
public class SchoolSetNewsServiceImpl extends ServiceImpl<SchoolSetNewsMapper, SchoolSetNews> implements ISchoolSetNewsService {
    @Override
    public Result queryPage(SetNewsREQ req) {
        // System.out.println("查询新闻");
        LambdaQueryWrapper<SchoolSetNews> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(req.getTitle())){
            lambdaQueryWrapper.like(SchoolSetNews::getTitle, req.getTitle());

        }
        lambdaQueryWrapper.orderByDesc(SchoolSetNews::getId);
        return Result.ok(baseMapper.selectPage(req.getPage(), lambdaQueryWrapper));
    }

    @Override
    public Result delete(String id) {
        SchoolSetNews setNews = baseMapper.selectById(id);
        if(setNews == null){
            return Result.error("操作失败,找不到要更新的新闻");
        }
        // 更新状态为下降
        setNews.setStatus("3");
        setNews.setDeletedTime(new Date());
        baseMapper.updateById(setNews);
        return Result.ok();
    }
}
